module ifu(nPC_sel,zero,clk,rst,instruction);
  input clk,rst;
  input [1:0]nPC_sel;           //选择跳转或顺序执行
  input [31:0]zero;
  output [31:0]instruction;
  
  reg [31:0]pc;
  reg [31:0]im[2047:0];         //2KB指令存储器
  reg [31:0]pcnew;
  reg [31:0]extout;             //扩展并左移2位后的地址
  wire [31:0]temp,t0,t1;
  wire [15:0]imm16;
  
  //PC指向11位的2KB指令存储器，4个8位数拼接
  assign instruction = {im[pc[10:0]],im[pc[10:0]]+1,im[pc[10:0]]+2,im[pc[10:0]]+3};
  
  //16立即数扩展成32位
  assign temp = {{16{imm16[15]}},imm16};
  
  assign t0 = pc+4;              
  assign t1 = t0+extout;        //跳转地址
  
  always@(*)begin
    extout = temp[31:0] << 2;
  end
  
  always@(*)begin
    if(nPC_sel == 0)
      pcnew = t0;
    else
      pcnew = t1;
  end
  
  always@(posedge clk or posedge rst)begin
    if(rst)
      pc <= 32'h0000_0000;
    else
      pc <= pcnew;
  end
endmodule 

